package com.project.web.controller.api;

import com.project.client.domain.MessageEntity;
import com.project.client.domain.UserEntity;
import com.project.client.service.MessageService;
import com.project.client.vo.MessageVo;
import com.project.common.annotation.Log;
import com.project.common.annotation.RepeatSubmit;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.core.domain.Result;
import com.project.common.core.page.TableDataInfo;
import com.project.common.enums.BusinessType;
import com.project.common.utils.ConvertUtils;
import com.project.framework.annotation.PersonalLogin;
import com.project.framework.annotation.PersonalLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/message")
public class ClientMessageController extends BaseController {
    @Autowired
    private MessageService service;

    //留言
    @RepeatSubmit
    @PersonalLogin
    @PostMapping("/push")
    public Result push(@PersonalLoginUser UserEntity user, @RequestBody @Validated MessageVo vo) {
        MessageEntity entity = ConvertUtils.sourceToTarget(vo, MessageEntity.class);
        entity.setUid(user.getId());
        return toResult(service.save(entity));
    }


    /**
     * 查询留言记录列表
     */
    @PreAuthorize("@ss.hasPermi('client:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(MessageEntity entity) {
        startPage();
        List<MessageEntity> list = service.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 获取留言记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('client:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(service.selectById(id));
    }

    /**
     * 删除留言记录
     */
    @PreAuthorize("@ss.hasPermi('client:message:remove')")
    @Log(title = "留言记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(service.delete(ids));
    }
}
